Skip to content

Release discarded SDK response bodies before retrying - #1770

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/retry-response-bodies
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/retry-response-bodies

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

The SDK's fetchWithRetry discards retryable HTTP responses without reading or cancelling their bodies. An unfinished error body can keep a connection occupied while the next request waits for it.

A local HTTP fixture reproduces this through startAgentRun: with a single-connection Undici dispatcher, an unending 503 body prevents the next request from reaching the server. Cancelling that body lets the retry succeed. This follows Undici's response-body ownership guidance.

Changes

Cancel a discarded response body before the existing backoff. Treat cancellation failure as best effort so it cannot replace the retry result. Responses returned to the caller remain readable.

Add one regression through startAgentRun, using real Response/ReadableStream objects and the existing fake retry clock. It checks cleanup order, a failing cancellation, and the eventual successful result.

Verification

  • The new regression plus the agent-step-batching, fetch-agent-publisher-trust, and delegated-user-header test files: main has 45 passed / 1 failed; with the patch, 46 passed / 0 failed and 1,252 assertions.
  • Native Node 24.20.0 HTTP probe: main times out with only one request received and the first response still open. The patch closes it and completes the second request.
  • Local probes confirm HTTP 400 and the final 503 after retry exhaustion still expose their diagnostic body; the request counts stay at one and four respectively.
  • SDK typecheck passes with the locked native TypeScript 7.0.2 compiler. Frozen-lockfile install passes. ESLint has 0 errors and 4 local resolver warnings. The new test is formatted; the source's existing Prettier differences are unchanged.
  • Raw SDK and binary builds fail because the locked declaration generator resolves TypeScript 7.0.2, which lacks the ts.sys API it imports. The same SDK failure occurs on main. A supplemental build with a temporary local JavaScript TypeScript mapping reached declaration generation but exhausted Bun memory; the original dependency was restored. No build or smoke pass is claimed.

Notes / Scope

Two SDK files, no dependency or public API changes. Retry statuses, attempt limits, backoff, and request replay policy are unchanged. This patch only releases responses that the retry loop already discards.

Validation used Windows, Bun 1.3.14, and local fixtures without credentials. Other operating systems, production endpoints, and the full monorepo suite were not run. The declaration-build dependency issue remains outside this patch.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a minimal, well-targeted fix. In sdk/src/impl/database.ts, the retry path previously discarded the Response from a retryable HTTP error without reading or cancelling its body — under Undici's connection-pooling model this can leave a connection occupied by an abandoned stream, exactly as described in the linked Undici docs. Adding await response.body?.cancel().catch(() => {}) before the existing backoff is the right remedy: it's best-effort (failures don't affect the retry result), it doesn't touch the body of responses actually returned to the caller, and it doesn't change retry policy, status handling, or backoff timing.

The new test in retry-response-body.test.ts is a reasonable regression: it uses real Response/ReadableStream objects, the existing fake retry clock, and asserts both cancellation ordering and that a throwing cancel() doesn't derail the eventual successful result. That's the right level of test for this kind of fix.

A few things worth tightening before porting:

  • The body text of the diagnostic 503 is still logged via logger?.warn with only status/attempt/url — confirm nothing downstream expects to read the retryable response body for diagnostics after this change (your notes say HTTP 400/exhausted-503 bodies remain readable, but worth double-checking all call sites).
  • The PR description is very long with build/typecheck environment noise unrelated to the actual change; a maintainer mainly needs the two-file diff and the test rationale.

Overall: narrow, in-scope (sdk/ only), with a clear cause and a matching test — good candidate for porting as-is or with minor logging review.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants